package com.szqhyxj.youxiangju.biz.plan.preview;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.plan.PlanCreateSuccessActivity;
import com.szqhyxj.youxiangju.biz.plan.PlanDetail;
import com.szqhyxj.youxiangju.biz.plan.PlanModel;
import com.szqhyxj.youxiangju.biz.plan.TasksItem;
import com.szqhyxj.youxiangju.biz.plan.adapter.PlanPreviewAdapter;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.popview.MsgChoicePopView;
import com.szqhyxj.youxiangju.sys.components.view.popview.SingleImgDialog;
import com.yxj.pay.planMaking.model.Task;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import butterknife.BindDrawable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.http.HEAD;

public class PlanPreviewActivity extends BaseActivity implements PlanPreviewContract.View, NavigationBar.NavigationBarListener{

    @BindView(R.id.tasks)
    ExpandableListView tasks;
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.re_create)
    Button reCreate;
    @BindView(R.id.apply)
    Button apply;

    private HeaderViewHolder headerViewHolder;

    private String title;



    final class HeaderViewHolder{
        @BindView(R.id.repayment_amount)
        TextView repaymentAmount;
        @BindView(R.id.working_fund)
        TextView workingFund;
        @BindView(R.id.reimbursement_charge)
        TextView reimbursementCharge;
        @BindView(R.id.number)
        TextView number;
        @BindDrawable(R.mipmap.zhouzhuanjin_bg)
        Drawable img;
        View view;

        @OnClick(R.id.shuoming_zzj)
        public void onViewClicked(){
            SingleImgDialog dialog = new SingleImgDialog(PlanPreviewActivity.this);
            dialog.setImg(img);
            dialog.show();
        }

        public View getView(){
            return view;
        }

        HeaderViewHolder(View view){
            ButterKnife.bind(this, view);
            this.view = view;
        }
    }



    private static final String TAG = PlanPreviewActivity.class.getSimpleName();

    @Override
    public void showPlan(PlanDetail planDetail){

        headerViewHolder.repaymentAmount.setText(String.format(Locale.getDefault(), "%.2f", planDetail.getTotalAmount())+"元");
        headerViewHolder.reimbursementCharge.setText(String.format(Locale.getDefault(), "%.2f", planDetail.getPayCostTotal())+"元");
        headerViewHolder.workingFund.setText(String.format(Locale.getDefault(), "%.2f", planDetail.getWorkingAmount())+"元");
        List<TasksItem> tasks = planDetail.getTasks();
        int repayNumber = 0;
        for(TasksItem task : tasks){
            if(task.getType() == Task.REPAY){
                repayNumber++;
            }
        }
        headerViewHolder.number.setText(String.valueOf(repayNumber)+"笔");
        List<List<Map<String, TasksItem>>> tasksList = new ArrayList<>();
        List<PlanModel.PlanParticularsDayEntity> entityList = new ArrayList<>();
        PlanModel.convertToDisplayPreview(planDetail, tasksList, entityList);
        this.tasks.setAdapter(new PlanPreviewAdapter(tasksList, entityList));
    }

    @Override
    public void showApplySuccess(PlanDetail planDetail){
        PlanCreateSuccessActivity.start(PlanPreviewActivity.this, planDetail);
    }

    public static void start(Activity activity, String planId, PlanModel.PlanCreateEntity planCreateEntity){
        Intent intent = new Intent();
        intent.putExtra(PlanPreviewContract.PLAN_ID, planId);
        intent.putExtra(PlanPreviewContract.PARAM, planCreateEntity);
        intent.setClass(activity, PlanPreviewActivity.class);
        activity.startActivity(intent);
    }

    private PlanPreviewContract.Presenter presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_plan);
        ButterKnife.bind(this);
        PlanModel.PlanCreateEntity planCreateEntity = getIntent().getParcelableExtra(PlanPreviewContract.PARAM);
        title = planCreateEntity.getBtName();
        presenter = new PlanPreviewPresenter(this);
        presenter.start(getIntent());
        View headerView = LayoutInflater.from(this).inflate(R.layout.plan_formulate_header_layout, null);

        headerViewHolder = new HeaderViewHolder(headerView);

        reCreate.setOnClickListener(v -> presenter.reCreatePlan());
        apply.setOnClickListener(v -> onClick());
        this.tasks.addHeaderView(headerViewHolder.getView());
        navigationBar.setNavigationBarListener(this);
        navigationBar.setNaviTitle(title);
    }

    public void onClick(){
        MsgChoicePopView popView = new MsgChoicePopView(this);
        popView.setMsg("确定执行" + title + "的还款计划？");
        popView.setCancel("下次再说");
        popView.setConfirm("确定执行");
        popView.setOnClickListener(new MsgChoicePopView.OnClickListener(){
            @Override
            public void onClickConfirm(View v){
                presenter.applyPlan();
            }
        });
        popView.show();
    }

    @Override
    public void navigationLeft(){
        finish();
    }

    @Override
    public void navigationRight(){
    }
}